package com.yunxi.service.dubboService.impl.sto;

import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.model.sto.StoVrfidHistory;
import com.yunxi.service.localService.sto.StoVrfidHistoryService;
import com.yunxi.sto.pojo.StoVrfidHistoryVO;
import com.yunxi.sto.service.StoVrfidHistoryDubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 车辆RFID绑定历史 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-07-23
 */
public class StoVrfidHistoryDubboServiceImpl implements StoVrfidHistoryDubboService {

    @Autowired
    @Qualifier(value = "stoVrfidHistoryServiceImpl")
    StoVrfidHistoryService stoVrfidHistoryService;


    @Override
    public StoVrfidHistoryVO getById(String id) throws Exception {
        StoVrfidHistory stoVrfidHistory = stoVrfidHistoryService.getById(id);
        if(stoVrfidHistory == null){
            throw new Exception("操作失败,无法根据id:"+id+"找到对应历史!");
        }
        StoVrfidHistoryVO stoVrfidHistoryVO = new StoVrfidHistoryVO();
        BeanUtils.copyProperties(stoVrfidHistory, stoVrfidHistoryVO);
        return stoVrfidHistoryVO;
    }

    @Override
    public void saveOrUpdate(StoVrfidHistoryVO stoVrfidHistoryVO, LoginInfo loginInfo) throws Exception {
        setPartnerCode(stoVrfidHistoryVO, loginInfo);
        StoVrfidHistory stoVrfidHistory = new StoVrfidHistory();
        BeanUtils.copyProperties(stoVrfidHistoryVO, stoVrfidHistory);
        stoVrfidHistoryService.saveOrUpdate(stoVrfidHistory);
    }

    @Override
    public void removeById(String id) {
        stoVrfidHistoryService.removeById(id);
    }

    @Override
    public PageInfo<StoVrfidHistoryVO> findPageByVO(StoVrfidHistoryVO stoVrfidHistoryVO, Integer pageNum, Integer pageSize,LoginInfo loginInfo) throws Exception {
        setPartnerCode(stoVrfidHistoryVO, loginInfo);
        PageInfo<StoVrfidHistoryVO> pageInfo = stoVrfidHistoryService.pageByVo(stoVrfidHistoryVO, pageNum, pageSize);
        return pageInfo;
    }

    @Override
    public void removeByListVo(List<StoVrfidHistoryVO> stoVrfidHistoryVOList) throws Exception {
        stoVrfidHistoryService.removeByListVo(stoVrfidHistoryVOList);
    }

    @Override
    public List listByVo(StoVrfidHistoryVO stoVrfidHistoryVO) {
        return stoVrfidHistoryService.listByVo(stoVrfidHistoryVO);
    }

    /**
     * 设置渠道商代码
     * @param stoVrfidHistoryVO
     * @param loginInfo
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    private void setPartnerCode(StoVrfidHistoryVO stoVrfidHistoryVO, LoginInfo loginInfo) throws IllegalAccessException, InvocationTargetException {
        Object partner = loginInfo.getPartner();
        if(partner != null){
            PartnerVO partnerVO = new PartnerVO();
            org.apache.commons.beanutils.BeanUtils.copyProperties(partnerVO,partner);
            stoVrfidHistoryVO.setPartnerCode(partnerVO.getPartnerCode());
        }
    }
}
